home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / jpeg / jrdjfif.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  23KB  |  843 lines

  1. /*
  2.  * jrdjfif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to decode standard JPEG file headers/markers.
  9.  * This code will handle "raw JPEG" and JFIF-convention JPEG files.
  10.  *
  11.  * You can also use this module to decode a raw-JPEG or JFIF-standard data
  12.  * stream that is embedded within a larger file.  To do that, you must
  13.  * position the file to the JPEG SOI marker (0xFF/0xD8) that begins the
  14.  * data sequence to be decoded.  If nothing better is possible, you can scan
  15.  * the file until you see the SOI marker, then use JUNGETC to push it back.
  16.  *
  17.  * This module relies on the JGETC macro and the read_jpeg_data method (which
  18.  * is provided by the user interface) to read from the JPEG data stream.
  19.  * Therefore, this module is not dependent on any particular assumption about
  20.  * the data source; it need not be a stdio stream at all.  (This fact does
  21.  * NOT carry over to more complex JPEG file formats such as JPEG-in-TIFF;
  22.  * those format control modules may well need to assume stdio input.)
  23.  *
  24.  * These routines are invoked via the methods read_file_header,
  25.  * read_scan_header, read_jpeg_data, read_scan_trailer, and read_file_trailer.
  26.  */
  27.  
  28. #include "jinclude.h"
  29.  
  30. #ifdef JFIF_SUPPORTED
  31.  
  32.  
  33. typedef enum {            /* JPEG marker codes */
  34.   M_SOF0  = 0xc0,
  35.   M_SOF1  = 0xc1,
  36.   M_SOF2  = 0xc2,
  37.   M_SOF3  = 0xc3,
  38.   
  39.   M_SOF5  = 0xc5,
  40.   M_SOF6  = 0xc6,
  41.   M_SOF7  = 0xc7,
  42.   
  43.   M_JPG   = 0xc8,
  44.   M_SOF9  = 0xc9,
  45.   M_SOF10 = 0xca,
  46.   M_SOF11 = 0xcb,
  47.   
  48.   M_SOF13 = 0xcd,
  49.   M_SOF14 = 0xce,
  50.   M_SOF15 = 0xcf,
  51.   
  52.   M_DHT   = 0xc4,
  53.   
  54.   M_DAC   = 0xcc,
  55.   
  56.   M_RST0  = 0xd0,
  57.   M_RST1  = 0xd1,
  58.   M_RST2  = 0xd2,
  59.   M_RST3  = 0xd3,
  60.   M_RST4  = 0xd4,
  61.   M_RST5  = 0xd5,
  62.   M_RST6  = 0xd6,
  63.   M_RST7  = 0xd7,
  64.   
  65.   M_SOI   = 0xd8,
  66.   M_EOI   = 0xd9,
  67.   M_SOS   = 0xda,
  68.   M_DQT   = 0xdb,
  69.   M_DNL   = 0xdc,
  70.   M_DRI   = 0xdd,
  71.   M_DHP   = 0xde,
  72.   M_EXP   = 0xdf,
  73.   
  74.   M_APP0  = 0xe0,
  75.   M_APP15 = 0xef,
  76.   
  77.   M_JPG0  = 0xf0,
  78.   M_JPG13 = 0xfd,
  79.   M_COM   = 0xfe,
  80.   
  81.   M_TEM   = 0x01,
  82.   
  83.   M_ERROR = 0x100
  84. } JPEG_MARKER;
  85.  
  86.  
  87. /*
  88.  * Reload the input buffer after it's been emptied, and return the next byte.
  89.  * This is exported for direct use by the entropy decoder.
  90.  * See the JGETC macro for calling conditions.  Note in particular that
  91.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  92.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  93.  * For error recovery purposes, synthesizing an EOI marker is probably best.
  94.  *
  95.  * For this header control module, read_jpeg_data is supplied by the
  96.  * user interface.  However, header formats that require random access
  97.  * to the input file would need to supply their own code.  This code is
  98.  * left here to indicate what is required.
  99.  */
  100.  
  101. #if 0                /* not needed in this module */
  102.  
  103. METHODDEF int
  104. read_jpeg_data (decompress_info_ptr cinfo)
  105. {
  106.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  107.  
  108.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  109.                     cinfo->next_input_byte,
  110.                     JPEG_BUF_SIZE);
  111.   
  112.   if (cinfo->bytes_in_buffer <= 0) {
  113.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  114.     cinfo->next_input_byte[0] = (char) 0xFF;
  115.     cinfo->next_input_byte[1] = (char) M_EOI;
  116.     cinfo->bytes_in_buffer = 2;
  117.   }
  118.  
  119.   return JGETC(cinfo);
  120. }
  121.  
  122. #endif
  123.  
  124.  
  125. /*
  126.  * Routines to parse JPEG markers & save away the useful info.
  127.  */
  128.  
  129.  
  130. LOCAL INT32
  131. get_2bytes (decompress_info_ptr cinfo)
  132. /* Get a 2-byte unsigned integer (e.g., a marker parameter length field) */
  133. {
  134.   INT32 a;
  135.   
  136.   a = JGETC(cinfo);
  137.   return (a << 8) + JGETC(cinfo);
  138. }
  139.  
  140.  
  141. LOCAL void
  142. skip_variable (decompress_info_ptr cinfo, int code)
  143. /* Skip over an unknown or uninteresting variable-length marker */
  144. {
  145.   INT32 length;
  146.   
  147.   length = get_2bytes(cinfo);
  148.   
  149.   TRACEMS2(cinfo->emethods, 1,
  150.        "Skipping marker 0x%02x, length %u", code, (int) length);
  151.   
  152.   for (length -= 2; length > 0; length--)
  153.     (void) JGETC(cinfo);
  154. }
  155.  
  156.  
  157. LOCAL void
  158. get_dht (decompress_info_ptr cinfo)
  159. /* Process a DHT marker */
  160. {
  161.   INT32 length;
  162.   UINT8 bits[17];
  163.   UINT8 huffval[256];
  164.   int i, index, count;
  165.   HUFF_TBL **htblptr;
  166.   
  167.   length = get_2bytes(cinfo)-2;
  168.   
  169.   while (length > 0) {
  170.     index = JGETC(cinfo);
  171.  
  172.     TRACEMS1(cinfo->emethods, 1, "Define Huffman Table 0x%02x", index);
  173.       
  174.     bits[0] = 0;
  175.     count = 0;
  176.     for (i = 1; i <= 16; i++) {
  177.       bits[i] = (UINT8) JGETC(cinfo);
  178.       count += bits[i];
  179.     }
  180.  
  181.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  182.          bits[1], bits[2], bits[3], bits[4],
  183.          bits[5], bits[6], bits[7], bits[8]);
  184.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  185.          bits[9], bits[10], bits[11], bits[12],
  186.          bits[13], bits[14], bits[15], bits[16]);
  187.  
  188.     if (count > 256)
  189.       ERREXIT(cinfo->emethods, "Bogus DHT counts");
  190.  
  191.     for (i = 0; i < count; i++)
  192.       huffval[i] = (UINT8) JGETC(cinfo);
  193.  
  194.     length -= 1 + 16 + count;
  195.  
  196.     if (index & 0x10) {        /* AC table definition */
  197.       index -= 0x10;
  198.       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  199.     } else {            /* DC table definition */
  200.       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  201.     }
  202.  
  203.     if (index < 0 || index >= NUM_HUFF_TBLS)
  204.       ERREXIT1(cinfo->emethods, "Bogus DHT index %d", index);
  205.  
  206.     if (*htblptr == NULL)
  207.       *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  208.   
  209.     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  210.     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  211.     }
  212. }
  213.  
  214.  
  215. LOCAL void
  216. get_dac (decompress_info_ptr cinfo)
  217. /* Process a DAC marker */
  218. {
  219.   INT32 length;
  220.   int index, val;
  221.  
  222.   length = get_2bytes(cinfo)-2;
  223.   
  224.   while (length > 0) {
  225.     index = JGETC(cinfo);
  226.     val = JGETC(cinfo);
  227.  
  228.     TRACEMS2(cinfo->emethods, 1,
  229.          "Define Arithmetic Table 0x%02x: 0x%02x", index, val);
  230.  
  231.     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  232.       ERREXIT1(cinfo->emethods, "Bogus DAC index %d", index);
  233.  
  234.     if (index >= NUM_ARITH_TBLS) { /* define AC table */
  235.       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  236.     } else {            /* define DC table */
  237.       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  238.       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  239.       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  240.     ERREXIT1(cinfo->emethods, "Bogus DAC value 0x%x", val);
  241.     }
  242.  
  243.     length -= 2;
  244.   }
  245. }
  246.  
  247.  
  248. LOCAL void
  249. get_dqt (decompress_info_ptr cinfo)
  250. /* Process a DQT marker */
  251. {
  252.   INT32 length;
  253.   int n, i, prec;
  254.   UINT16 tmp;
  255.   QUANT_TBL_PTR quant_ptr;
  256.   
  257.   length = get_2bytes(cinfo) - 2;
  258.   
  259.   while (length > 0) {
  260.     n = JGETC(cinfo);
  261.     prec = n >> 4;
  262.     n &= 0x0F;
  263.  
  264.     TRACEMS2(cinfo->emethods, 1,
  265.          "Define Quantization Table %d  precision %d", n, prec);
  266.  
  267.     if (n >= NUM_QUANT_TBLS)
  268.       ERREXIT1(cinfo->emethods, "Bogus table number %d", n);
  269.       
  270.     if (cinfo->quant_tbl_ptrs[n] == NULL)
  271.       cinfo->quant_tbl_ptrs[n] = (QUANT_TBL_PTR)
  272.     (*cinfo->emethods->alloc_small) (SIZEOF(QUANT_TBL));
  273.     quant_ptr = cinfo->quant_tbl_ptrs[n];
  274.  
  275.     for (i = 0; i < DCTSIZE2; i++) {
  276.       tmp = JGETC(cinfo);
  277.       if (prec)
  278.     tmp = (tmp<<8) + JGETC(cinfo);
  279.       quant_ptr[i] = tmp;
  280.     }
  281.  
  282.     for (i = 0; i < DCTSIZE2; i += 8) {
  283.       TRACEMS8(cinfo->emethods, 2, "        %4u %4u %4u %4u %4u %4u %4u %4u",
  284.            quant_ptr[i  ], quant_ptr[i+1], quant_ptr[i+2], quant_ptr[i+3],
  285.            quant_ptr[i+4], quant_ptr[i+5], quant_ptr[i+6], quant_ptr[i+7]);
  286.     }
  287.  
  288.     length -= DCTSIZE2+1;
  289.     if (prec) length -= DCTSIZE2;
  290.   }
  291. }
  292.  
  293.  
  294. LOCAL void
  295. get_dri (decompress_info_ptr cinfo)
  296. /* Process a DRI marker */
  297. {
  298.   if (get_2bytes(cinfo) != 4)
  299.     ERREXIT(cinfo->emethods, "Bogus length in DRI");
  300.  
  301.   cinfo->restart_interval = (UINT16) get_2bytes(cinfo);
  302.  
  303.   TRACEMS1(cinfo->emethods, 1,
  304.        "Define Restart Interval %u", cinfo->restart_interval);
  305. }
  306.  
  307.  
  308. LOCAL void
  309. get_app0 (decompress_info_ptr cinfo)
  310. /* Process an APP0 marker */
  311. {
  312. #define JFIF_LEN 14
  313.   INT32 length;
  314.   UINT8 b[JFIF_LEN];
  315.   int buffp;
  316.  
  317.   length = get_2bytes(cinfo) - 2;
  318.  
  319.   /* See if a JFIF APP0 marker is present */
  320.  
  321.   if (length >= JFIF_LEN) {
  322.     for (buffp = 0; buffp < JFIF_LEN; buffp++)
  323.       b[buffp] = (UINT8) JGETC(cinfo);
  324.     length -= JFIF_LEN;
  325.  
  326.     if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
  327.       /* Found JFIF APP0 marker: check version */
  328.       /* Major version must be 1 */
  329.       if (b[5] != 1)
  330.     ERREXIT2(cinfo->emethods, "Unsupported JFIF revision number %d.%02d",
  331.          b[5], b[6]);
  332.       /* Minor version should be 0..2, but try to process anyway if newer */
  333.       if (b[6] > 2)
  334.     TRACEMS2(cinfo->emethods, 1, "Warning: unknown JFIF revision number %d.%02d",
  335.          b[5], b[6]);
  336.       /* Save info */
  337.       cinfo->density_unit = b[7];
  338.       cinfo->X_density = (b[8] << 8) + b[9];
  339.       cinfo->Y_density = (b[10] << 8) + b[11];
  340.       /* Assume colorspace is YCbCr, unless UI has overridden me */
  341.       if (cinfo->jpeg_color_space == CS_UNKNOWN)
  342.     cinfo->jpeg_color_space = CS_YCbCr;
  343.       TRACEMS3(cinfo->emethods, 1, "JFIF APP0 marker, density %dx%d  %d",
  344.            cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  345.       if (b[12] | b[13])
  346.     TRACEMS2(cinfo->emethods, 1, "    with %d x %d thumbnail image",
  347.          b[12], b[13]);
  348.       if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  349.     TRACEMS1(cinfo->emethods, 1,
  350.          "Warning: thumbnail image size does not match data length %u",
  351.          (int) length);
  352.     } else {
  353.       TRACEMS1(cinfo->emethods, 1, "Unknown APP0 marker (not JFIF), length %u",
  354.            (int) length + JFIF_LEN);
  355.     }
  356.   } else {
  357.     TRACEMS1(cinfo->emethods, 1, "Short APP0 marker, length %u", (int) length);
  358.   }
  359.  
  360.   while (length-- > 0)        /* skip any remaining data */
  361.     (void) JGETC(cinfo);
  362. }
  363.  
  364.  
  365. LOCAL void
  366. get_sof (decompress_info_ptr cinfo, int code)
  367. /* Process a SOFn marker */
  368. {
  369.   INT32 length;
  370.   short ci;
  371.   int c;
  372.   jpeg_component_info * compptr;
  373.   
  374.   length = get_2bytes(cinfo);
  375.   
  376.   cinfo->data_precision = JGETC(cinfo);
  377.   cinfo->image_height   = get_2bytes(cinfo);
  378.   cinfo->image_width    = get_2bytes(cinfo);
  379.   cinfo->num_components = JGETC(cinfo);
  380.  
  381.   TRACEMS4(cinfo->emethods, 1,
  382.        "Start Of Frame 0x%02x: width=%u, height=%u, components=%d",
  383.        code, (int) cinfo->image_width, (int) cinfo->image_height,
  384.        cinfo->num_components);
  385.  
  386.   /* We don't support files in which the image height is initially specified */
  387.   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  388.   /* might as well have a general sanity check. */
  389.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  390.       || cinfo->num_components <= 0)
  391.     ERREXIT(cinfo->emethods, "Empty JPEG image (DNL not supported)");
  392.  
  393. #ifdef EIGHT_BIT_SAMPLES
  394.   if (cinfo->data_precision != 8)
  395.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  396. #endif
  397. #ifdef TWELVE_BIT_SAMPLES
  398.   if (cinfo->data_precision != 12) /* this needs more thought?? */
  399.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  400. #endif
  401. #ifdef SIXTEEN_BIT_SAMPLES
  402.   if (cinfo->data_precision != 16) /* this needs more thought?? */
  403.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  404. #endif
  405.  
  406.   if (length != (cinfo->num_components * 3 + 8))
  407.     ERREXIT(cinfo->emethods, "Bogus SOF length");
  408.  
  409.   cinfo->comp_info = (jpeg_component_info *) (*cinfo->emethods->alloc_small)
  410.             (cinfo->num_components * SIZEOF(jpeg_component_info));
  411.   
  412.   for (ci = 0; ci < cinfo->num_components; ci++) {
  413.     compptr = &cinfo->comp_info[ci];
  414.     compptr->component_index = ci;
  415.     compptr->component_id = JGETC(cinfo);
  416.     c = JGETC(cinfo);
  417.     compptr->h_samp_factor = (c >> 4) & 15;
  418.     compptr->v_samp_factor = (c     ) & 15;
  419.     compptr->quant_tbl_no  = JGETC(cinfo);
  420.       
  421.     TRACEMS4(cinfo->emethods, 1, "    Component %d: %dhx%dv q=%d",
  422.          compptr->component_id, compptr->h_samp_factor,
  423.          compptr->v_samp_factor, compptr->quant_tbl_no);
  424.   }
  425. }
  426.  
  427.  
  428. LOCAL void
  429. get_sos (decompress_info_ptr cinfo)
  430. /* Process a SOS marker */
  431. {
  432.   INT32 length;
  433.   int i, ci, n, c, cc;
  434.   jpeg_component_info * compptr;
  435.   
  436.   length = get_2bytes(cinfo);
  437.   
  438.   n = JGETC(cinfo);  /* Number of components */
  439.   cinfo->comps_in_scan = n;
  440.   length -= 3;
  441.   
  442.   if (length != (n * 2 + 3) || n < 1 || n > MAX_COMPS_IN_SCAN)
  443.     ERREXIT(cinfo->emethods, "Bogus SOS length");
  444.  
  445.   TRACEMS1(cinfo->emethods, 1, "Start Of Scan: %d components", n);
  446.   
  447.   for (i = 0; i < n; i++) {
  448.     cc = JGETC(cinfo);
  449.     c = JGETC(cinfo);
  450.     length -= 2;
  451.     
  452.     for (ci = 0; ci < cinfo->num_components; ci++)
  453.       if (cc == cinfo->comp_info[ci].component_id)
  454.     break;
  455.     
  456.     if (ci >= cinfo->num_components)
  457.       ERREXIT(cinfo->emethods, "Invalid component number in SOS");
  458.     
  459.     compptr = &cinfo->comp_info[ci];
  460.     cinfo->cur_comp_info[i] = compptr;
  461.     compptr->dc_tbl_no = (c >> 4) & 15;
  462.     compptr->ac_tbl_no = (c     ) & 15;
  463.     
  464.     TRACEMS3(cinfo->emethods, 1, "    c%d: [dc=%d ac=%d]", cc,
  465.          compptr->dc_tbl_no, compptr->ac_tbl_no);
  466.   }
  467.   
  468.   while (length > 0) {
  469.     (void) JGETC(cinfo);
  470.     length--;
  471.   }
  472. }
  473.  
  474.  
  475. LOCAL void
  476. get_soi (decompress_info_ptr cinfo)
  477. /* Process an SOI marker */
  478. {
  479.   int i;
  480.   
  481.   TRACEMS(cinfo->emethods, 1, "Start of Image");
  482.  
  483.   /* Reset all parameters that are defined to be reset by SOI */
  484.  
  485.   for (i = 0; i < NUM_ARITH_TBLS; i++) {
  486.     cinfo->arith_dc_L[i] = 0;
  487.     cinfo->arith_dc_U[i] = 1;
  488.     cinfo->arith_ac_K[i] = 5;
  489.   }
  490.   cinfo->restart_interval = 0;
  491.  
  492.   cinfo->density_unit = 0;    /* set default JFIF APP0 values */
  493.   cinfo->X_density = 1;
  494.   cinfo->Y_density = 1;
  495.  
  496.   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling */
  497. }
  498.  
  499.  
  500. LOCAL int
  501. next_marker (decompress_info_ptr cinfo)
  502. /* Find the next JPEG marker */
  503. /* Note that the output might not be a valid marker code, */
  504. /* but it will never be 0 or FF */
  505. {
  506.   int c, nbytes;
  507.  
  508.   nbytes = 0;
  509.   do {
  510.     do {            /* skip any non-FF bytes */
  511.       nbytes++;
  512.       c = JGETC(cinfo);
  513.     } while (c != 0xFF);
  514.     do {            /* skip any duplicate FFs */
  515.       /* we don't increment nbytes here since extra FFs are legal */
  516.       c = JGETC(cinfo);
  517.     } while (c == 0xFF);
  518.   } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  519.  
  520.   if (nbytes != 1)
  521.     WARNMS2(cinfo->emethods,
  522.         "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
  523.         nbytes-1, c);
  524.  
  525.   return c;
  526. }
  527.  
  528.  
  529. LOCAL JPEG_MARKER
  530. process_tables (decompress_info_ptr cinfo)
  531. /* Scan and process JPEG markers that can appear in any order */
  532. /* Return when an SOI, EOI, SOFn, or SOS is found */
  533. {
  534.   int c;
  535.  
  536.   while (TRUE) {
  537.     c = next_marker(cinfo);
  538.       
  539.     switch (c) {
  540.     case M_SOF0:
  541.     case M_SOF1:
  542.     case M_SOF2:
  543.     case M_SOF3:
  544.     case M_SOF5:
  545.     case M_SOF6:
  546.     case M_SOF7:
  547.     case M_JPG:
  548.     case M_SOF9:
  549.     case M_SOF10:
  550.     case M_SOF11:
  551.     case M_SOF13:
  552.     case M_SOF14:
  553.     case M_SOF15:
  554.     case M_SOI:
  555.     case M_EOI:
  556.     case M_SOS:
  557.       return ((JPEG_MARKER) c);
  558.       
  559.     case M_DHT:
  560.       get_dht(cinfo);
  561.       break;
  562.       
  563.     case M_DAC:
  564.       get_dac(cinfo);
  565.       break;
  566.       
  567.     case M_DQT:
  568.       get_dqt(cinfo);
  569.       break;
  570.       
  571.     case M_DRI:
  572.       get_dri(cinfo);
  573.       break;
  574.       
  575.     case M_APP0:
  576.       get_app0(cinfo);
  577.       break;
  578.  
  579.     case M_RST0:        /* these are all parameterless */
  580.     case M_RST1:
  581.     case M_RST2:
  582.     case M_RST3:
  583.     case M_RST4:
  584.     case M_RST5:
  585.     case M_RST6:
  586.     case M_RST7:
  587.     case M_TEM:
  588.       TRACEMS1(cinfo->emethods, 1, "Unexpected marker 0x%02x", c);
  589.       break;
  590.  
  591.     default:    /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn */
  592.       skip_variable(cinfo, c);
  593.       break;
  594.     }
  595.   }
  596. }
  597.  
  598.  
  599.  
  600. /*
  601.  * Initialize and read the file header (everything through the SOF marker).
  602.  */
  603.  
  604. METHODDEF void
  605. read_file_header (decompress_info_ptr cinfo)
  606. {
  607.   int c;
  608.  
  609.   /* Demand an SOI marker at the start of the file --- otherwise it's
  610.    * probably not a JPEG file at all.  If the user interface wants to support
  611.    * nonstandard headers in front of the SOI, it must skip over them itself
  612.    * before calling jpeg_decompress().
  613.    */
  614.   if (JGETC(cinfo) != 0xFF  ||  JGETC(cinfo) != M_SOI)
  615.     ERREXIT(cinfo->emethods, "Not a JPEG file");
  616.  
  617.   get_soi(cinfo);        /* OK, process SOI */
  618.  
  619.   /* Process markers until SOF */
  620.   c = process_tables(cinfo);
  621.  
  622.   switch (c) {
  623.   case M_SOF0:
  624.   case M_SOF1:
  625.     get_sof(cinfo, c);
  626.     cinfo->arith_code = FALSE;
  627.     break;
  628.       
  629.   case M_SOF9:
  630.     get_sof(cinfo, c);
  631.     cinfo->arith_code = TRUE;
  632.     break;
  633.  
  634.   default:
  635.     ERREXIT1(cinfo->emethods, "Unsupported SOF marker type 0x%02x", c);
  636.     break;
  637.   }
  638.  
  639.   /* Figure out what colorspace we have */
  640.   /* (too bad the JPEG committee didn't provide a real way to specify this) */
  641.  
  642.   switch (cinfo->num_components) {
  643.   case 1:
  644.     cinfo->jpeg_color_space = CS_GRAYSCALE;
  645.     break;
  646.  
  647.   case 3:
  648.     /* if we saw a JFIF marker, leave it set to YCbCr; */
  649.     /* also leave it alone if UI has provided a value */
  650.     if (cinfo->jpeg_color_space == CS_UNKNOWN) {
  651.       short cid0 = cinfo->comp_info[0].component_id;
  652.       short cid1 = cinfo->comp_info[1].component_id;
  653.       short cid2 = cinfo->comp_info[2].component_id;
  654.  
  655.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  656.     cinfo->jpeg_color_space = CS_YCbCr; /* assume it's JFIF w/out marker */
  657.       else if (cid0 == 1 && cid1 == 4 && cid2 == 5)
  658.     cinfo->jpeg_color_space = CS_YIQ; /* prototype's YIQ matrix */
  659.       else {
  660.     TRACEMS3(cinfo->emethods, 1,
  661.          "Unrecognized component IDs %d %d %d, assuming YCbCr",
  662.          cid0, cid1, cid2);
  663.     cinfo->jpeg_color_space = CS_YCbCr;
  664.       }
  665.     }
  666.     break;
  667.  
  668.   case 4:
  669.     cinfo->jpeg_color_space = CS_CMYK;
  670.     break;
  671.  
  672.   default:
  673.     cinfo->jpeg_color_space = CS_UNKNOWN;
  674.     break;
  675.   }
  676. }
  677.  
  678.  
  679. /*
  680.  * Read the start of a scan (everything through the SOS marker).
  681.  * Return TRUE if find SOS, FALSE if find EOI.
  682.  */
  683.  
  684. METHODDEF boolean
  685. read_scan_header (decompress_info_ptr cinfo)
  686. {
  687.   int c;
  688.   
  689.   /* Process markers until SOS or EOI */
  690.   c = process_tables(cinfo);
  691.   
  692.   switch (c) {
  693.   case M_SOS:
  694.     get_sos(cinfo);
  695.     return TRUE;
  696.     
  697.   case M_EOI:
  698.     TRACEMS(cinfo->emethods, 1, "End Of Image");
  699.     return FALSE;
  700.  
  701.   default:
  702.     ERREXIT1(cinfo->emethods, "Unexpected marker 0x%02x", c);
  703.     break;
  704.   }
  705.   return FALSE;            /* keeps lint happy */
  706. }
  707.  
  708.  
  709. /*
  710.  * The entropy decoder calls this routine if it finds a marker other than
  711.  * the restart marker it was expecting.  (This code is *not* used unless
  712.  * a nonzero restart interval has been declared.)  The passed parameter is
  713.  * the marker code actually found (might be anything, except 0 or FF).
  714.  * The desired restart marker is that indicated by cinfo->next_restart_num.
  715.  * This routine is supposed to apply whatever error recovery strategy seems
  716.  * appropriate in order to position the input stream to the next data segment.
  717.  * For some file formats (eg, TIFF) extra information such as tile boundary
  718.  * pointers may be available to help in this decision.
  719.  *
  720.  * This implementation is substantially constrained by wanting to treat the
  721.  * input as a data stream; this means we can't back up.  (For instance, we
  722.  * generally can't fseek() if the input is a Unix pipe.)  Therefore, we have
  723.  * only the following actions to work with:
  724.  *   1. Do nothing, let the entropy decoder resume at next byte of file.
  725.  *   2. Read forward until we find another marker, discarding intervening
  726.  *      data.  (In theory we could look ahead within the current bufferload,
  727.  *      without having to discard data if we don't find the desired marker.
  728.  *      This idea is not implemented here, in part because it makes behavior
  729.  *      dependent on buffer size and chance buffer-boundary positions.)
  730.  *   3. Push back the passed marker (with JUNGETC).  This will cause the
  731.  *      entropy decoder to process an empty data segment, inserting dummy
  732.  *      zeroes, and then re-read the marker we pushed back.
  733.  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  734.  * appropriate if the found marker is a future restart marker (indicating
  735.  * that we have missed the desired restart marker, probably because it got
  736.  * corrupted).
  737.  
  738.  * We apply #2 or #3 if the found marker is a restart marker no more than
  739.  * two counts behind or ahead of the expected one.  We also apply #2 if the
  740.  * found marker is not a legal JPEG marker code (it's certainly bogus data).
  741.  * If the found marker is a restart marker more than 2 counts away, we do #1
  742.  * (too much risk that the marker is erroneous; with luck we will be able to
  743.  * resync at some future point).
  744.  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
  745.  * overrunning the end of a scan.  An implementation limited to single-scan
  746.  * files might find it better to apply #2 for markers other than EOI, since
  747.  * any other marker would have to be bogus data in that case.
  748.  */
  749.  
  750. METHODDEF void
  751. resync_to_restart (decompress_info_ptr cinfo, int marker)
  752. {
  753.   int desired = cinfo->next_restart_num;
  754.   int action = 1;
  755.  
  756.   /* Always put up a warning. */
  757.   WARNMS2(cinfo->emethods,
  758.       "Corrupt JPEG data: found 0x%02x marker instead of RST%d",
  759.       marker, desired);
  760.   /* Outer loop handles repeated decision after scanning forward. */
  761.   for (;;) {
  762.     if (marker < M_SOF0)
  763.       action = 2;        /* invalid marker */
  764.     else if (marker < M_RST0 || marker > M_RST7)
  765.       action = 3;        /* valid non-restart marker */
  766.     else {
  767.       if (marker == (M_RST0 + ((desired+1) & 7)) ||
  768.       marker == (M_RST0 + ((desired+2) & 7)))
  769.     action = 3;        /* one of the next two expected restarts */
  770.       else if (marker == (M_RST0 + ((desired-1) & 7)) ||
  771.            marker == (M_RST0 + ((desired-2) & 7)))
  772.     action = 2;        /* a prior restart, so advance */
  773.       else
  774.     action = 1;        /* desired restart or too far away */
  775.     }
  776.     TRACEMS2(cinfo->emethods, 4,
  777.          "At marker 0x%02x, recovery action %d", marker, action);
  778.     switch (action) {
  779.     case 1:
  780.       /* Let entropy decoder resume processing. */
  781.       return;
  782.     case 2:
  783.       /* Scan to the next marker, and repeat the decision loop. */
  784.       marker = next_marker(cinfo);
  785.       break;
  786.     case 3:
  787.       /* Put back this marker & return. */
  788.       /* Entropy decoder will be forced to process an empty segment. */
  789.       JUNGETC(marker, cinfo);
  790.       JUNGETC(0xFF, cinfo);
  791.       return;
  792.     }
  793.   }
  794. }
  795.  
  796.  
  797. /*
  798.  * Finish up after a compressed scan (series of read_jpeg_data calls);
  799.  * prepare for another read_scan_header call.
  800.  */
  801.  
  802. METHODDEF void
  803. read_scan_trailer (decompress_info_ptr cinfo)
  804. {
  805.   /* no work needed */
  806. }
  807.  
  808.  
  809. /*
  810.  * Finish up at the end of the file.
  811.  */
  812.  
  813. METHODDEF void
  814. read_file_trailer (decompress_info_ptr cinfo)
  815. {
  816.   /* no work needed */
  817. }
  818.  
  819.  
  820. /*
  821.  * The method selection routine for standard JPEG header reading.
  822.  * Note that this must be called by the user interface before calling
  823.  * jpeg_decompress.  When a non-JFIF file is to be decompressed (TIFF,
  824.  * perhaps), the user interface must discover the file type and call
  825.  * the appropriate method selection routine.
  826.  */
  827.  
  828. GLOBAL void
  829. jselrjfif (decompress_info_ptr cinfo)
  830. {
  831.   cinfo->methods->read_file_header = read_file_header;
  832.   cinfo->methods->read_scan_header = read_scan_header;
  833.   /* For JFIF/raw-JPEG format, the user interface supplies read_jpeg_data. */
  834. #if 0
  835.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  836. #endif
  837.   cinfo->methods->resync_to_restart = resync_to_restart;
  838.   cinfo->methods->read_scan_trailer = read_scan_trailer;
  839.   cinfo->methods->read_file_trailer = read_file_trailer;
  840. }
  841.  
  842. #endif /* JFIF_SUPPORTED */
  843.